package com.guozcc.bootweb.platform.common;

import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static final String YYYYMMDD_HHMMSS = "yyyyMMdd HHmmss";

    public static final String YYYY_MM_DD = "yyyy-MM-dd";

    public static final String YYYYMMDD = "yyyyMMdd";

    public static final String yyyyMMddHHmmssSSS = "yyyyMMddHHmmssSSS";

    public static String date2String(Date date,String format){
        return new SimpleDateFormat(format).format(date);
    }

    public static Date string2Date(String dateStr,String format) throws Exception {
        return new SimpleDateFormat(format).parse(dateStr);
    }

    public static long date2long(Date date){
        return date.getTime();
    }

    public static String long2DateStr(long date,String format) throws Exception {
        return new SimpleDateFormat(format).format(date);
    }

    public static Date long2Date(long date,String format) throws Exception {
        return string2Date(new SimpleDateFormat(format).format(date),format);
    }

    public static String getCurrentDateStr(String format){
        return date2String(new Date(), StringUtils.isEmpty(format)?YYYY_MM_DD_HH_MM_SS:format);
    }

    public static Date addDays(Date date,int days){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)+days);
        return calendar.getTime();
    }

//    public static void main(String[] args) throws Exception {
//        System.out.println(getCurrentDateStr(YYYY_MM_DD_HH_MM_SS));
//        System.out.println(new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).format(System.currentTimeMillis()) );
//
//        Date date = string2Date("2023-10-16 15:57:59",YYYY_MM_DD_HH_MM_SS);
//        System.out.println(date.toString());
//        System.out.println(addDays(date,-1));
//        System.out.println(addDays(date,3));
//    }
}
